﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Ajax.Common
{
    /// <summary>
    ///  string类扩展
    /// </summary>
    public class MyString
    {
        #region 返回指定字符串的单字节长度
        /// <summary>
        /// 返回指定字符串的单字节长度
        /// </summary>
        /// <param name="strInPut"></param>
        /// <returns></returns>
        public static int Length(string strInPut)
        {
            Encoding encoding = Encoding.Default;
            byte[] strbytes = encoding.GetBytes(strInPut);
            return strbytes.Length;
        }
        #endregion

        #region  截短字串的函数，兼容中英文
        /// <summary>
        /// 截短字串的函数，兼容中英文
        /// </summary>
        /// <param name="mText">要加工的字串</param>
        /// <param name="byteCount">长度</param>
        /// <returns>被加工过的字串</returns>
        public static string Left(string mText, int byteCount)
        {
            if (byteCount < 1)
                return mText;

            if (System.Text.Encoding.Default.GetByteCount(mText) <= byteCount)
            {
                return mText;
            }
            else
            {
                byte[] txtBytes = System.Text.Encoding.Default.GetBytes(mText);
                byte[] newBytes = new byte[byteCount - 4];

                for (int i = 0; i < byteCount - 4; i++)
                {
                    newBytes[i] = txtBytes[i];
                }
                string OutPut = System.Text.Encoding.Default.GetString(newBytes) + "...";
                if (OutPut.EndsWith("?...") == true)
                {
                    OutPut = OutPut.Substring(0, OutPut.Length - 4);
                    OutPut += "...";
                }
                return OutPut;
            }
        }
        #endregion

        #region  截短字串的函数，不分区中英文
        /// <summary>
        /// 截短字串的函数，不分区中英文
        /// </summary>
        /// <param name="InPut"></param>
        /// <param name="Long"></param>
        /// <returns></returns>
        public static string Left(object InPut, int Long)
        {
            if (Long >= InPut.ToString().Length)
            {
                return InPut.ToString();
            }
            else
            {
                string OutPut = InPut.ToString().Substring(0, Long - 3) + "...";
                if (OutPut.EndsWith("?...") == true)
                {
                    OutPut = OutPut.Substring(0, OutPut.Length - 4);
                    OutPut += "...";
                }
                return OutPut;
            }
        }
        #endregion

        #region 去除非法字串
        /// <summary>
        /// 去除非法字串
        /// </summary>
        /// <param name="InPut">原字串</param>
        /// <returns>过滤过的字串</returns>
        public static string ReplaceBadChar(string InPut)
        {
            if (InPut.Trim().Length == 0)
            {
                return string.Empty;
            }
            else
            {
                StringBuilder OutPut = new StringBuilder();
                OutPut.Append(InPut);
                OutPut.Replace("'", "");
                OutPut.Replace("*", "");
                OutPut.Replace("?", "");
                OutPut.Replace("(", "");
                OutPut.Replace(")", "");
                OutPut.Replace("<", "");
                OutPut.Replace("=", "");

                return OutPut.ToString().Trim();
            }
        }
        #endregion

        #region 转义SQL中的限制符号
        /// <summary>
        /// 转义SQL中的限制符号
        /// </summary>
        /// <param name="InPut"></param>
        /// <returns></returns>
        public static string SQL_ESC(string InPut)
        {
            StringBuilder OutPut = new StringBuilder();
            OutPut.Append(InPut);
            OutPut.Replace("[", "[[]");
            OutPut.Replace("%", "[%]");
            OutPut.Replace("_", "[_]");
            OutPut.Replace("'", "''");

            return OutPut.ToString().Trim();
        }
        #endregion

        #region 替换中文数字输入字符
        /// <summary>
        /// 替换中文数字输入字符
        /// </summary>
        /// <param name="strIn">输入字符</param>
        /// <returns></returns>
        public static string ReplaceString(string strIn)
        {
            string strOut = strIn;
            strOut = strOut.Replace("０", "0");
            strOut = strOut.Replace("１", "1");
            strOut = strOut.Replace("２", "2");
            strOut = strOut.Replace("３", "3");
            strOut = strOut.Replace("４", "4");
            strOut = strOut.Replace("５", "5");
            strOut = strOut.Replace("６", "6");
            strOut = strOut.Replace("７", "7");
            strOut = strOut.Replace("８", "8");
            strOut = strOut.Replace("９", "9");
            strOut = strOut.Replace("．", ".");

            return strOut;
        }
        #endregion

        #region 删除多余的空行
        /// <summary>
        /// 删除多余的空行
        /// </summary>
        /// <param name="InPut"></param>
        /// <returns></returns>
        public static string DeleteBlankLine(string InPut)
        {
            if (InPut.Length > 0)
            {
                return InPut = Regex.Replace(InPut, @"(\n[\s| ]*\r)", "");
            }
            else
            {
                return InPut;
            }
        }
        #endregion

        #region 格式化ＸＭＬ禁用符号
        /// <summary>
        /// 格式化ＸＭＬ禁用符号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public string FormatForXML(string input)
        {
            StringBuilder OutPut = new StringBuilder();
            OutPut.Append(input);
            OutPut.Replace("<", "&lt;");
            OutPut.Replace(">", "&gt");
            OutPut.Replace("&", "&amp;");
            OutPut.Replace("\"", "&quot;");
            OutPut.Replace("'", "&apos;");
            OutPut.Replace(" ", "#x20");

            return OutPut.ToString();
        }

        #endregion


        /// <summary>
        /// 取得开始、结束字符之间的字符串极其本身
        /// </summary>
        /// <param name="Input"></param>
        /// <param name="startTag"></param>
        /// <param name="endTag"></param>
        /// <returns></returns>
        public static string GetString(string Input, string startTag, string endTag)
        {
            int s = Input.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase);
            if (s < 0)
                throw new System.ArgumentOutOfRangeException("开始标记不存在");



            int e = Input.IndexOf(endTag, s + startTag.Length, StringComparison.CurrentCultureIgnoreCase);


            return Input.Substring(s, e + endTag.Length - s).Trim();
        }

        /// <summary>
        /// 取得开始、结束字符之间的字符串
        /// </summary>
        /// <param name="Input"></param>
        /// <param name="startTag"></param>
        /// <param name="endTag"></param>
        /// <returns></returns>
        public static string GetInnerString(string Input, string startTag, string endTag)
        {
            //int s = Input.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase);
            //if (s < 0)
            //    throw new System.ArgumentOutOfRangeException("开始标记不存在");

            //int e = Input.IndexOf(endTag, s + startTag.Length, StringComparison.CurrentCultureIgnoreCase);

            //return Input.Substring(s + startTag.Length, e - s - startTag.Length).Trim();


            string str = "";
            int index = Input.IndexOf(startTag, 0, StringComparison.CurrentCultureIgnoreCase);
            if (index >= 0)
            {
                int i = Input.IndexOf(endTag, index, StringComparison.CurrentCultureIgnoreCase);
                if (i >= 0)
                {
                    str = Input.Substring(index + startTag.Length, (i - index) - startTag.Length);
                }
            }
            return str;
        }
    }
}